package cn.zhqr.core.utils;

import cn.zhqr.common.config.GlobalConstants;
import cn.zhqr.common.config.RiskConfig;
import cn.zhqr.db.model.StationSetting;
import cn.zhqr.db.model.UserWashCard;
import cn.zhqr.db.model.WashCard;

import java.math.BigDecimal;

public class WashCardKit {
    /**
     * 获取洗车可用余额，次/月卡为单次最大金额
     * @param userWashCard
     * @param stationSetting
     * @return
     */
    public static BigDecimal getBalance(UserWashCard userWashCard, StationSetting stationSetting){
        BigDecimal maxAmount = userWashCard.getBalance();//本次最大消费金额.
        if(userWashCard.getCardType()== WashCard.TYPE_TIMES || userWashCard.getCardType()==WashCard.TYPE_MONTH){
            if(stationSetting!=null && stationSetting.getCardOnceMaxAmount().compareTo(BigDecimal.ZERO)>0){
                maxAmount = stationSetting.getCardOnceMaxAmount();
            }
            if(maxAmount==null || maxAmount.compareTo(BigDecimal.ZERO)<=0){
                maxAmount = GlobalConstants.CARD_ONCE_MAX_AMOUNT;
            }
        }
        return maxAmount;
    }

    /**
     * 获取单次最大金额
     * @param userWashCard
     * @param stationSetting
     * @return
     */
    public static BigDecimal getMaxOrderAmount(UserWashCard userWashCard,StationSetting stationSetting){
        BigDecimal maxAmount = RiskConfig.ORDER_MAX_AMOUNT;
        if(userWashCard.getCardType()== WashCard.TYPE_TIMES || userWashCard.getCardType()==WashCard.TYPE_MONTH){
            if(stationSetting!=null){
                if(stationSetting.getCardOnceMaxAmount().compareTo(BigDecimal.ZERO)>0){
                    maxAmount = maxAmount.min(stationSetting.getCardOnceMaxAmount());
                }
                if(stationSetting.getOrderMaxAmount().compareTo(BigDecimal.ZERO)>0){
                    maxAmount = maxAmount.min(stationSetting.getOrderMaxAmount());
                }
            }
            maxAmount = maxAmount.min(GlobalConstants.CARD_ONCE_MAX_AMOUNT);
        }
        return maxAmount;
    }

    /**
     * 获取单次最大时长
     * @param userWashCard
     * @param stationSetting
     * @return
     */
    public static Long getMaxOrderTime(UserWashCard userWashCard,StationSetting stationSetting){
        Long maxTime = RiskConfig.ORDER_MAX_TIME;   //由风控规则定义的绝对最大时长，任何设置不得超过此设置
        if(userWashCard.getCardType()== WashCard.TYPE_TIMES || userWashCard.getCardType()==WashCard.TYPE_MONTH){
            maxTime = GlobalConstants.DEFAULT_CARD_ONCE_MAX_TIME; //默认的次卡/月卡单次最大时长
        }

        //读取网点配置
        if(stationSetting != null){
            //网点单个订单最大洗车时长
            if(stationSetting.getOrderMaxTime()>0 && stationSetting.getOrderMaxTime()<RiskConfig.ORDER_MAX_TIME){
                maxTime = stationSetting.getOrderMaxTime();
            }

            //如果次卡/月卡有单独配置，则覆盖OrderMaxTime；如未配置，则使用OrderMaxTime
            if(userWashCard.getCardType()== WashCard.TYPE_TIMES || userWashCard.getCardType()==WashCard.TYPE_MONTH){
                if(stationSetting.getCardOnceMaxTime() > 0 && stationSetting.getCardOnceMaxTime()<RiskConfig.ORDER_MAX_TIME){
                    maxTime = stationSetting.getCardOnceMaxTime();
                }
            }
        }

        return maxTime;
    }
}
